<template>
    <div class="page borderbox absolute t0 w100">
        <div v-for="(item,index) in address" :key="index" class="addressList flex flexSpaceB whiteBg br8 p10 borderbox" @click="addList(item)">
            <div class="flex col textL" style="width:75%;">
                <span class="fs16 bold mt6 mb4">{{item.contact}}</span>
                <p class="fs14 counp lh18">收货地址：{{item.address}}</p>
                <p class="fs14 counp lh18">联系电话：{{item.phone}}</p>
            </div>
            <!-- <x-icon type="ios-arrow-forward" size="30"></x-icon> -->
            <div class="flex col h100 flexJCA w50 h70">
                <div class="flex flexSpaceB h26" @click.stop="()=>{}">
                    <img v-if="item.isDefault == '0'" class="w14" src="../../../images/Check@2xS.png" alt="">
                    <img v-if="item.isDefault == '1'" class="w15" src="../../../images/Check@2x.png" alt="">
                    <span class="fs14 grayText w30">默认</span>
                </div>
                <div class="flex flexSpaceB h26" @click.stop="editAddress(item.id)">
                    <img class="w14" src="../../../images/bianji1.png" alt="">
                    <span class="fs14 grayText w30">编辑</span>
                </div>
                <div class="flex flexSpaceB h26" @click.stop="sureDelete(item.id,index)">
                    <img class="w14" src="../../../images/shanchu1.png" alt="">
                    <span class="fs14 grayText w30">删除</span>
                </div>
            </div>
        </div>
        <show-bg-img :isShow="showBg" title="暂无收货地址"></show-bg-img>
        <div style="height:6rem;"></div>
        <div class="BottonButton vux-1px-t">
            <span @click="addNew">添加新地址</span>
        </div>
        <popup-bcs :isShow="isShow" title="确定删除？" @cancel="()=>{}" @confirm="deleteAddress"></popup-bcs>
    </div>
</template>
<script>
//从商品结算页跳转过来时  type为0   从地址管理进入时 type为1
import { getAddressList,deleteAddressById } from 'request';
import { Toast } from "mint-ui";
export default {
    name:'addressMangeSet',
    data(){
        return {
            address:[],
            showBg:false,
            isShow:false,
            id:'',
            index:'',
        }
    },
    created(){
        this.getAddress();       //获取地址信息
    },
    computed:{
        // 判断当前进入类型   从地址管理进入时type 为1 详情页进入时，type为空默认为0
        type(){
            return this.$route.query.type || '0';
        }
    },
    methods:{
        //获取地址信息
        getAddress(){
            this.$vux.loading.show({ text: 'loading' })
            getAddressList({}).then(res=>{
                this.$vux.loading.hide();
                if(res.rspCode == '0000' && res.data.rspCode == '0000'){
                    let arr = [...res.data.addressList];
                    if(arr.length != 0){
                        let ind;
                        arr.forEach((item,index)=>{
                            item.isDefault == '0' && (ind = index)
                        });
                        let A = arr.splice(ind,1);
                        this.address = A.concat(arr)
                    }
                    this.showBg = this.address.length == 0 ? true : false;
                }else{
                    Toast({message:'地址信息获取失败',position:'middle',duration:1500});
                }
            }).catch(err=>{
                this.$vux.loading.hide();
            })
        },
        //点击返回上一页更改地址
        addList(item){
            if(this.type == '0'){
                this.$store.commit('addCateId',item.id)
                this.$router.go(-1)
            }
        },
        //添加新地址
        addNew(){
            this.$router.push({ path: "/addAddress",query:{ type: this.type}});
        },
        //编辑地址
        editAddress(id){
            this.$router.push({ path: '/addAddress',query: {id: id}});
        },
        sureDelete(id,index){
            this.isShow = true;
            this.id = id;
            this.index = index;
        },
        //删除地址
        deleteAddress(){
            deleteAddressById({id:this.id}).then(res=>{
                if(res.rspCode=='0000' && res.data.rspCode == "0000"){
                    Toast({message:'删除成功',position:'middle',duration:1500});
                    this.isShow = false;
                    this.address.splice(this.index,1);
                    this.showBg = this.address.length == 0 ? true : false;
                }else{
                    Toast({message:'删除失败',position:'middle',duration:1500});
                }
            }).catch(err=>{
                console.log(err)
            })
        }
    }
}
</script>
<style lang="less" scoped>
.counp{
    color:#939298;
}
.vux-x-icon {
  fill: #c7c6cb;
}
.page{
    background:#f6f6f6;
    min-height:100%;
    .addressList{
        width:94%;
        margin-left:3%;
        margin-top:1rem;
    }
}
</style>
